var $ = {
    ajax: function (obj) {
        var host = window.location.host,
            port = window.location.port,
            portocol = window.location.portocol;
        var url = obj.url,
            data = obj.data || '',
            type = obj.type,
            dataType = obj.dataType,
            async = obj.async || true,
            success = obj.success,
            error = obj.error,
            beforeSend = obj.beforeSend || function () { },
            timeOut = obj.timOut;
        var urlArr = url.split('/');
        var timer = null;
        var lock;
        if (dataType == 'jsonp') {
            lock = host == urlArr[2] && portocol == urlArr[0] ? true : false;
            if (lock) {
                var xhr;
                if (window.XMLHttpRequest) {
                    xhr = new XMLHttpRequest();
                } else {
                    xhr = new ActiveXObject('Microsoft.XMLHttp');
                }
                type = type.toUpperCase();
                if (type == 'GET') {
                    xhr.open(type, url + '?' + data, async)
                    xhr.send();
                } else if (type == 'POST') {
                    xhr.open(type, url.async);
                    xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                    xhr.send(data);
                }

                xhr.onreadystatechange = function () {

                    if (xhr.readyState == 4 && xhr.status == 200) {
                        success(xhr.responseText);
                    } else {
                        beforeSend();
                    }
                }
            } else {
                window.success = success;
                var oScr = document.createElement('script');
                oScr.src = url;
                document.body.appendChild(oScr);

            }
        } else {
            var xhr;
            if (window.XMLHttpRequest) {
                xhr = new XMLHttpRequest();
            } else {
                xhr = new ActiveXObject('Microsoft.XMLHttp');
            }
            type = type.toUpperCase();
            if (type == 'GET') {
                xhr.open(type, url + '?' + data, async)
                xhr.send();
            } else if (type == 'POST') {
                xhr.open(type, url.async);
                xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
                xhr.send(data);
            }
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    success(xhr.responseText);
                } else {
                    beforeSend();
                }
            }
        }
    }
}


jsonP = (function () {
    var oJson;
    return function (obj) {
        if (oJson) {
            document.body.removeChild(oJson);
        }
        oJson = document.createElement('script');
        oJson.src = obj.url + obj.val + obj.callback;
        document.body.appendChild(oJson);
    }
}())

// $.ajax({
//     type: 'GET',
//     url: 'https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=a&cb=success',
//     dataType: 'jsonp',
//     success: function (data) {
//         console.log(data);
//     },
//     beforeSend: function () {
//         console.log('wocao');
//     }
// })